// 首页文章列表
<template>
  <el-card class="article">
    <el-row>
      <el-col :span="8">
        <div class="article-cover">
          <router-link :to="{name: 'ArticleDetail',params: { id: article.id} }">
            <img :src="article.cover" alt="没有图片">
          </router-link>
        </div>
      </el-col>
      <el-col :span="16">
        <div class="info">
          <router-link :to="{name: 'ArticleDetail',params: { id: article.id} }">
            <div v-if="article.title.length > 30" class="article-title">{{ article.title.slice(0,27)+ '...' }}</div>
            <div v-else class="article-title"> {{ article.title }} </div>
            <div class="article-time">{{ formatTime }} {{ article.username }}</div>
            <div class="article-content">{{ brief }}</div>
          </router-link>
        </div>
        <el-tag v-for="type in article.type" :key="type.id" size="small" class="article-tag"> {{ type.name }} </el-tag>
      </el-col>
    </el-row>
  </el-card>
</template>

<script>
export default {
  name: 'Article',
  // 接收父组件传来的 article 对象
  props: {
    article: Object()
  },
  // 计算属性
  computed: {
    // 对时间进行格式化
    formatTime: function() {
      if (this.article) {
        const dt = new Date(this.article.createTime)
        const month = dt.getMonth() + 1
        const date = dt.getDate()
        return `${month}月${date}日`
      }
      return ''
    },
    // 截取文章内容的前 35 个字，并加上省略号
    brief: function() {
      return this.article.description + '...'
    }
  }
}
</script>

<style lang="scss" scoped>
.article {
  width: 500px;
  height: 200px;
  display: flex;
  float: left;
  margin-left: 30px;
  margin-top: 30px;
  padding: 10px 0;
  .article-title {
    font-size: 20px;
    font-weight: 500;
    color: #574266;
    text-align: justify;
    white-space:normal;
         word-break:break-all;
         word-wrap:break-word;
  }
  .article-cover {
    flex: none;
    width: 120px;
    height: 120px;
    margin-right: 10px;
    img {
      width: 100%;
    }
  }
  .article-content, .article-time {
    font-size: 14px;
  }
  .article-time {
    margin: 10px 0;
    color: #c5c5c5;
  }
  .el-row {
    width: 458px;
  }
}

.article-tag{
  margin-top: 10px;
}
</style>

